package com.xuecheng.test.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 主要用作类别ActiveMQ中的订阅模式,RabbitMQ实现
 * RabbitMQ是一个消费者绑定一个队列,一定要和ActiveMQ的主题订阅模式区分开
 *
 */
public class 测试ActiveMQ的订阅生产 {
    public static final String exchange = "ActiveMQ_Topic";

    public static void main(String[] args) {
        // 通过链接工程创建链接
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        Connection connection = null;
        try {
            connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
            // 前往别指定队列名,系统会随机生成
            channel.queueDeclare("",true,false,false,null);
            // 只需要保证路由,和路由key一样, 那么绑定了这个路由和路由key的消息,都可以消费到这条消息
            // 其实就是把消息发送到了路由下的所有队列
            channel.basicPublish(exchange,"a",null,"测试".getBytes());
        } catch (TimeoutException | IOException e) {
            e.printStackTrace();
        }
    }
}
